<?php defined('SYSPATH') or die('No direct script access.');

class FlashMessage
{
	/**
	 * Type of message
	 */
	const ERROR = 'error';
	const WARNING = 'warning';
	const NOTICE = 'notice';
	const ATTENTION = 'attention';
	const INFORMATION = 'information';
	const SUCCESS = 'success';

	protected $messages;
	protected $type;
	private static $instance;

	public function __construct(){
		if(!is_null(Session::instance()->get('fmessage',null))){
			$this->messages = unserialize(Session::instance()->get('fmessage'));
		}
	}

	public static function getInstance(){
		if(!isset(self::$instance))
			self::$instance = new FlashMessage();
		
		return self::$instance;
	}

	/**
	 * parse error cho doctrine
	 */
	public function doctrine($params)
	{
		/*
		array
		  'first_name' => 
		    array
		      0 => string 'notblank' (length=8)
		      1 => string 'email' (length=5)
		  'email' => 
		    array
		      0 => string 'email' (length=5)
      	*/
      	
		if (is_array($params))
		{
			foreach($params as $field => $rules)
			{
				$field = __($field); // translate field name
				$values = array(
					':field' => $field,
				);

				$this->add(array(
					self::ERROR => __(Kohana::message('validation', $rules[0]), $values),
				));
			}
			
		}
		else
		{
			//success
			$this->add(array(
				self::SUCCESS => 'thao tác thành công',
			));

		}

		
	}

	/**
	 * @param $message type : array
	 * @example : Array(
	 *		FlashMessage::ERROR => 'u have an error here',
	 *		FlashMessage::WARNING => 'u have another warning too',
	 *	 )
	 * @param $isNew If you want to begin new session, this mean your message won't merge with old message in session
	 *			old message in stack will be deleted
	 */
	public function add($message,$type = self::ERROR,$isNew = false){
		if($isNew){
			$this->refresh();
		}

		// neu truyen vao thong bao ma ko co type
		// thi mac dinh coi nhu do la thong bao loi
		if(is_string($message))
		{
			$temp[$type] = $message;
			$message = $temp;		
			unset($temp);
		}
		

		//append new message to messages
		foreach($message as $type => $msg){
			$this->messages[$type][] = $msg;
		}
		$this->serialize();
		
		return $this;
	}

	/**
	 * If you need to render your message in html view
	 * Return html view for this message
	 */
	public function display($template = 'flashmessage/render'){

		// neu ko co messages thi khoi display
		if (count($this->messages) === 0)
			return FALSE;
			
		if( strpos($template, 'flashmessage') === FALSE)
		{
			$template = 'flashmessage/' . $template;	
		}
		

		$view = View::factory($template)->set(array(
				'messages' => $this->messages,
				'type' => $this->type
			)); 

		//clear current message in session 
		$this->refresh();

		return $view->render();
	}
	
	private function serialize(){
		Session::instance()->set('fmessage',serialize($this->messages));
	}

	/**
	 * Clear sesion and renew one
	 */
	private function refresh(){
		Session::instance()->delete('fmessage');
	}

	/**
	 * Serialize  the message to session before destruct this class
	 */
	public function __destruct(){
		Session::instance()->set('fmessage',serialize($this->messages));
	}
}